package com.oocl.im.clinet;

import java.io.EOFException;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.net.SocketException;
import java.util.Date;
import java.util.Queue;
import java.util.concurrent.BlockingQueue;

import javax.management.Query;

import com.oocl.im.gui.ChatFrame;
import com.oocl.im.packet.MsgPackage;

public class ReceiverThread implements Runnable {
	Socket socket;
	ObjectInputStream in;
	ObjectOutputStream out;
	Client client;

	public ReceiverThread(Socket socket, ObjectInputStream in,
			ObjectOutputStream out, Client client) {
		super();
		this.socket = socket;
		this.in = in;
		this.out = out;
		this.client = client;
	}

	public void run() {
		try {
			while (true) {
				MsgPackage msg = (MsgPackage) in.readObject();
				System.out.println(msg);
				switch (msg.getOpeartion()) {
				case NOTIFY:
					client.notifyGui(msg);
					break;
				case CHAT:
					client.chatGui(msg);
					break;
				case SHAKE:
					client.shakeGui(msg);
					break;
				case EXIT:
					if(msg.getContent().equals("you has login ")){
						client.exitGui("You has already login !!");
					}
					client.exitGui(msg);
					break;
				default:
					break;
				}
			}
		} catch (EOFException | SocketException e) {
			if(client.getOnlineInfo()==null){
				client.exitGui("     You has already login !!");
			}else{
				System.out.println("socket has closed");
				client.exitGui("                    Disconnect !!");
				}
		}

		catch (Exception e) {
			e.printStackTrace();
		}
	}
}
